var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

const {
  expressjwt: expressjwt
} = require('express-jwt')

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
// admin
const login = require('./routes/login/login');
const router = require('./routes/login/getRouter')
const button = require('./routes/login/getButton')
const studentLogin = require('./routes/login/loginStudent')
const studentList = require('./routes/login/getStudentRouter')
const getBuild = require('./routes/admin/getBulid');
const getDianInfo = require('./routes/admin/getDianInfo')
const DelDian = require('./routes/admin/delDian')
const getTitle = require('./routes/admin/getTitle')
const DelTitle = require('./routes/admin/delTitle')
const InsertTitle = require('./routes/admin/insetTitle')
const DelAll = require('./routes/admin/delAll')
const Fabu = require('./routes/admin/Fabu')
const SelectTitle = require('./routes/admin/selectTileInfo')
const DataReport = require('./routes/admin/getDataReport')
const RoomNumber = require('./routes/admin/getRoomNumber')
const Cost = require('./routes/admin/getCost')
const editTitleInfo = require('./routes/admin/editTitleInfo')
const SelectStudent = require('./routes/admin/selectStudent')
const selectAdmin = require('./routes/admin/selectAadmin')
const updateAdmin = require('./routes/admin/updateAdminInfo')
const personNum = require('./routes/admin/getPerson')
const getDelare = require('./routes/admin/selectDedlare')
const getFloor = require('./routes/admin/getFloor')
const getRooms = require('./routes/admin/getRooms')
const addStudent = require('./routes/admin/addStudent')
const addCard = require('./routes/admin/addCard')
// student
const home = require("./routes/student/home")
const paylist = require('./routes/student/getPay')
const CardId = require('./routes/student/getCarId')
const Dian = require('./routes/student/getDianFei')
const Room = require('./routes/student/getRoom')
const BuildId = require('./routes/student/getBuildId')
const UpdataRoom = require('./routes/student/getUpdateRoom')
const PayFees = require('./routes/student/getPayFees')
const UpdatePayFees = require('./routes/student/UpdatePayFees')
const DianInfo = require('./routes/student/getStudentDian')
const shcoolCost = require('./routes/student/getSchoolCost')
const UpdateSchool = require('./routes/student/upDateSchool')
const LoginInfo = require('./routes/student/getLoginInfo')
const InsetBao  = require('./routes/student/InsetBao')
const updateLoginInfo = require('./routes/student/updateLoginInfo');
const getTitleInfo = require('./routes/student/selectTitleInfo');
const UpdateWater = require('./routes/student/updateWater')
const clearOrder = require('./routes/student/clearOrder')
const OrderCost = require('./routes/student/getCost');
const delStudent = require('./routes/admin/delStudent')
const getDeclareInfo = require('./routes/admin/getDeclareInfo')
const getDelareUpdate = require('./routes/admin/getDelareUpdate')
const addLiu = require('./routes/admin/addLiu')
const selectLui  = require('./routes/admin/selectLiu')
const updateLiu = require('./routes/admin/ipdateLiu')
const insertRoom = require('./routes/student/insertInfo')
const getDian = require('./routes/student/getDian')
const getOrderList = require('./routes/admin/getOrderList')
const getDeclareInfoAll = require('./routes/admin/getDeclareInfoAll')
// admin
const getDaiInfo = require('./routes/admin/getDaiInfo')
// 图片上传
const upload = require('./routes/upload/index')
var app = express();
var cors = require('cors')
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static('public'));
// app.use(expressjwt({
//   secret: 'login', //自己之前定义的密钥
//   algorithms: ['HS256'] //不加会报错（这里具体用什么值我还不太了解，本篇重点不在此）
// }).unless({
//   path: ['/api/Adminlogin', '/api/loginStudent'] //除了这个地址，其他的URL都需要验证token
// }));

app.use('/api', [
  // student
  login, router, button, studentLogin, studentList, home,
  paylist,CardId,Dian,Room,BuildId,UpdataRoom,PayFees,UpdatePayFees,
  DianInfo,shcoolCost,UpdateSchool,LoginInfo,InsetBao,upload,updateLoginInfo,getTitleInfo,
  UpdateWater,clearOrder,OrderCost,getDian,getOrderList,
  // admin
  getBuild,getDianInfo,DelDian,getTitle,DelTitle,InsertTitle,DelAll,Fabu,
  SelectTitle,DataReport,RoomNumber,Cost,
  editTitleInfo,SelectStudent,selectAdmin,updateAdmin,personNum,
  getDelare,getDaiInfo,getFloor,getRooms,addStudent,addCard,delStudent,getDeclareInfo
  ,getDelareUpdate,addLiu,selectLui,updateLiu,insertRoom,getDeclareInfoAll
])

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
